﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ro.fortech.training.mvc.control;
using Model;

namespace SilverlightView
{
    public partial class StartStopWork : UserControl
    {
        public StartStopWork()
        {
            InitializeComponent();
            btnStart.Click += btnStart_Click;
            btnStop.Click += btnStop_Click;
            btnLogout.Click += btnLogout_Click;
            LayoutRoot.Loaded += LayoutRoot_Loaded;
        }

        AccountController account = new AccountController();
        Controller control = new Controller();

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            
            account.UserWorking(AccountController.userLogged);
            TimeSpan minlate = control.GetMinutesLate(DateTime.Now);
            txtMinLate.Text = minlate.Hours.ToString() + "h " + minlate.Minutes.ToString() + "min";
            TimeSpan workduration = control.GetWorkDuration(DateTime.Now.TimeOfDay);
            txtWorkDuration.Text = workduration.Hours.ToString() + "h " + workduration.Minutes.ToString() + "min";
            StartEnable(AccountController.userLogged);
            StopEnable(AccountController.userLogged);
            
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {

           // account.UserNotWorking(AccountController.userLogged, DateTime.Now.TimeOfDay);
            TimeSpan workduration = control.GetWorkDuration(DateTime.Now.TimeOfDay);
            txtWorkDuration.Text = workduration.Hours.ToString()+"h "+workduration.Minutes.ToString()+"min";
            StartEnable(AccountController.userLogged);
            StopEnable(AccountController.userLogged);

        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            NavigateController screen = new NavigateController();
            if (screen.ShowLoginScreen())
                this.Content = new LoginScreen();            
        }

        private void StartEnable(User user)
        {
            if (user.IsWorking)
                btnStart.IsEnabled = false;
            else btnStart.IsEnabled = true; ;
        }
        private void StopEnable(User user)
        {
            if (!user.IsWorking)
                btnStop.IsEnabled = false;
            else btnStop.IsEnabled = true;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(AccountController.userLogged.MinLate))
            {
                TimeSpan minlate = TimeSpan.Parse(AccountController.userLogged.MinLate);
                txtMinLate.Text = minlate.Hours.ToString() + "h " + minlate.Minutes.ToString() + "min";
                TimeSpan workduration = control.GetWorkDuration(DateTime.Now.TimeOfDay);
                txtWorkDuration.Text = workduration.Hours.ToString() + "h " + workduration.Minutes.ToString() + "min";
            }                   
            StartEnable(AccountController.userLogged);
            StopEnable(AccountController.userLogged);

        }
    }
}
